﻿# -*- coding: utf-8 -*-
from google.appengine.ext import db

class Slogan(db.Model):
    content = db.StringProperty(required=True)

class LocalUser(db.Model):
        user = db.UserProperty(required = True)
        local_name = db.StringProperty()
        first_login_date = db.DateTimeProperty(auto_now_add=True)
        
        @property
        def nickname(self):
            return self.local_name or self.user.nickname()
            
        @property
        def email(self):
            return self.user.email()
         
class Image(db.Model):
        mime = db.StringProperty(required=False)
        data = db.BlobProperty(required=False)
        width = db.IntegerProperty(required=False)
        height = db.IntegerProperty(required=False)

class Entry(db.Model):
        comment = db.TextProperty(required=False)
        date = db.DateTimeProperty(auto_now_add=True)
        author = db.ReferenceProperty(LocalUser, required=False)

        image = db.ReferenceProperty(Image,required=False)
        image_url = db.StringProperty()
        image_purl = db.StringProperty()

        @property
        def replies(self, limit=30):
            return self.reply_set.order('-date').fetch(limit=limit)

        def delete(self):
                if self.image:
                    self.image.delete()
                replies = self.reply_set
                if replies:
                        for reply in replies:
                                reply.delete()
                super(Entry, self).delete()

class Reply(db.Model):
        comment = db.TextProperty(required=True)
        date = db.DateTimeProperty(auto_now_add=True)
        author = db.ReferenceProperty(LocalUser,required=False)
        entry = db.ReferenceProperty(Entry, required=True)

